package com.example.utils;

import cn.hutool.core.text.StrFormatter;
import org.springframework.util.AntPathMatcher;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Blob;
import java.sql.Clob;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils extends org.apache.commons.lang3.StringUtils {
    private static final String NULLSTR = "";
    private static final char SEPARATOR = '_';
    public static final String[] upperWord = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
    public static final String[] lowerWord = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    public static String[] num = new String[]{"2", "3", "4", "5", "6", "7", "8", "9"};
    public static String[] symbol = new String[]{"!", "@", "#", "$", "%", "^", "&", "*"};
    public static final SimpleDateFormat yyyyMM = new SimpleDateFormat("yyyyMM");
    public static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");

    public StringUtils() {
    }

    public static <T> T nvl(T value, T defaultValue) {
        return value != null ? value : defaultValue;
    }

    public static boolean isEmpty(Collection<?> coll) {
        return isNull(coll) || coll.isEmpty();
    }

    public static boolean isNotEmpty(Collection<?> coll) {
        return !isEmpty(coll);
    }

    public static boolean isEmpty(Object[] objects) {
        return isNull(objects) || objects.length == 0;
    }

    public static boolean isNotEmpty(Object[] objects) {
        return !isEmpty(objects);
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return isNull(map) || map.isEmpty();
    }

    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    public static boolean isEmpty(String str) {
        return isNull(str) || "".equals(str.trim());
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isNull(Object object) {
        return object == null;
    }

    public static boolean isNotNull(Object object) {
        return !isNull(object);
    }

    public static boolean isArray(Object object) {
        return isNotNull(object) && object.getClass().isArray();
    }

    public static String trim(String str) {
        return str == null ? "" : str.trim();
    }

    public static String substring(String str, int start) {
        if (str == null) {
            return "";
        } else {
            if (start < 0) {
                start += str.length();
            }

            if (start < 0) {
                start = 0;
            }

            return start > str.length() ? "" : str.substring(start);
        }
    }

    public static String substring(String str, int start, int end) {
        if (str == null) {
            return "";
        } else {
            if (end < 0) {
                end += str.length();
            }

            if (start < 0) {
                start += str.length();
            }

            if (end > str.length()) {
                end = str.length();
            }

            if (start > end) {
                return "";
            } else {
                if (start < 0) {
                    start = 0;
                }

                if (end < 0) {
                    end = 0;
                }

                return str.substring(start, end);
            }
        }
    }

    public static boolean hasText(String str) {
        return str != null && !str.isEmpty() && containsText(str);
    }

    private static boolean containsText(CharSequence str) {
        int strLen = str.length();

        for(int i = 0; i < strLen; ++i) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }

        return false;
    }

    public static String format(String template, Object... params) {
        return !isEmpty(params) && !isEmpty(template) ? StrFormatter.format(template, params) : template;
    }

    public static String toUnderScoreCase(String str) {
        if (str == null) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            boolean preCharIsUpperCase = true;
            boolean curreCharIsUpperCase = true;
            boolean nexteCharIsUpperCase = true;

            for(int i = 0; i < str.length(); ++i) {
                char c = str.charAt(i);
                if (i > 0) {
                    preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
                } else {
                    preCharIsUpperCase = false;
                }

                curreCharIsUpperCase = Character.isUpperCase(c);
                if (i < str.length() - 1) {
                    nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
                }

                if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
                    sb.append('_');
                } else if (i != 0 && !preCharIsUpperCase && curreCharIsUpperCase) {
                    sb.append('_');
                }

                sb.append(Character.toLowerCase(c));
            }

            return sb.toString();
        }
    }

    public static boolean inStringIgnoreCase(String str, String... strs) {
        if (str != null && strs != null) {
            String[] var2 = strs;
            int var3 = strs.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String s = var2[var4];
                if (str.equalsIgnoreCase(trim(s))) {
                    return true;
                }
            }
        }

        return false;
    }

    public static String convertToCamelCase(String name) {
        StringBuilder result = new StringBuilder();
        if (name != null && !name.isEmpty()) {
            if (!name.contains("_")) {
                return name.substring(0, 1).toUpperCase() + name.substring(1);
            } else {
                String[] camels = name.split("_");
                String[] var3 = camels;
                int var4 = camels.length;

                for(int var5 = 0; var5 < var4; ++var5) {
                    String camel = var3[var5];
                    if (!camel.isEmpty()) {
                        result.append(camel.substring(0, 1).toUpperCase());
                        result.append(camel.substring(1).toLowerCase());
                    }
                }

                return result.toString();
            }
        } else {
            return "";
        }
    }

    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        } else {
            s = s.toLowerCase();
            StringBuilder sb = new StringBuilder(s.length());
            boolean upperCase = false;

            for(int i = 0; i < s.length(); ++i) {
                char c = s.charAt(i);
                if (c == '_') {
                    upperCase = true;
                } else if (upperCase) {
                    sb.append(Character.toUpperCase(c));
                    upperCase = false;
                } else {
                    sb.append(c);
                }
            }

            return sb.toString();
        }
    }

    public static boolean matches(String str, List<String> strs) {
        if (!isEmpty(str) && !isEmpty((Collection)strs)) {
            Iterator var2 = strs.iterator();

            String pattern;
            do {
                if (!var2.hasNext()) {
                    return false;
                }

                pattern = (String)var2.next();
            } while(!isMatch(pattern, str));

            return true;
        } else {
            return false;
        }
    }

    public static boolean isMatch(String pattern, String url) {
        AntPathMatcher matcher = new AntPathMatcher();
        return matcher.match(pattern, url);
    }

    public static String removeSuffix(String str, String suffix) {
        if (null == str) {
            return null;
        } else if ("".equals(str.trim())) {
            return "";
        } else if (null != suffix && !"".equals(suffix)) {
            return str.endsWith(suffix) ? str.substring(0, str.length() - suffix.length()) : str;
        } else {
            return str;
        }
    }

    public static String subString(String str, int length) {
        if (isBlank(str)) {
            return "";
        } else {
            return str.length() > length ? str.substring(0, length) : str;
        }
    }

    public static boolean isBlank(String str) {
        if (null == str) {
            return true;
        } else {
            return "".equals(str.trim());
        }
    }

    public static boolean isBlank(Integer str) {
        return null == str;
    }

    public static boolean isBlank(Long str) {
        return null == str;
    }

    public static boolean isBlank(Double str) {
        return null == str;
    }

    public static boolean isBlank(Blob blob) {
        return blob == null;
    }

    public static boolean isBlank(Clob clob) {
        return clob == null;
    }

    public static boolean isBlank(Date date) {
        return null == date;
    }

    public static String toString(Object obj) {
        return obj == null ? "" : obj.toString().trim();
    }

    public static Long getLong(Object value) {
        Long lon = null;
        if (value != null) {
            if (value instanceof Long) {
                lon = (Long)value;
            } else if (value.toString().trim().length() > 0) {
                lon = new Long(value.toString().trim());
            }
        }

        return lon;
    }

    public static Double getDouble(Object value) {
        Double d = null;
        if (value != null && value.toString().trim().length() > 0) {
            d = new Double(value.toString().trim());
        }

        return d;
    }

    public static Float getFloat(Object value) {
        Float fla = null;
        if (value != null) {
            if (value instanceof Float) {
                fla = (Float)value;
            } else if (value.toString().trim().length() > 0) {
                fla = new Float(value.toString());
            }
        }

        return fla;
    }

    public static String getString(Object value) {
        String str = null;
        if (value != null && value.toString().trim().length() > 0) {
            str = value.toString().trim();
        }

        return str;
    }

    public static Date getDate(Object value) {
        Date date = null;
        if (value != null && value instanceof Date) {
            date = (Date)value;
        }

        return date;
    }

    public static Integer getInteger(Object value) {
        Integer integer = null;
        if (value != null && value.toString().trim().length() > 0) {
            integer = new Integer(value.toString().trim());
        }

        return integer;
    }

    public static int getInt(Object value) {
        int i = 0;
        if (value != null && value.toString().trim().length() > 0) {
            i = Integer.parseInt(value.toString().trim());
        }

        return i;
    }

    public static Boolean getBoolean(Object value) {
        Boolean b = null;
        if (value != null && value.toString().trim().length() > 0) {
            b = Boolean.parseBoolean(value.toString().trim());
        }

        return b;
    }

    public static long getlong(Object value) {
        long lon = 0L;
        if (value != null && value.toString().trim().length() > 0) {
            lon = Long.parseLong(value.toString().trim());
        }

        return lon;
    }

    public static String add(String str, int num) {
        int i = num;
        if (!isBlank(str)) {
            int intStr = Integer.parseInt(str);
            i = num + intStr;
        }

        return Integer.toString(i);
    }

    public static String getString(String str) {
        return null == str ? "" : str;
    }

    public static String getSum(String augend, String second, String addend) {
        if (augend == null) {
            augend = "0";
        }

        if (second == null) {
            second = "0";
        }

        if (addend == null) {
            addend = "0";
        }

        int sum = Integer.parseInt(augend) + Integer.parseInt(second) + Integer.parseInt(addend);
        return (new Integer(sum)).toString();
    }

    public static String change(String str, int n, boolean isLeft) {
        if (str != null && str.length() < n) {
            String s = "";

            for(int i = str.length(); i < n; ++i) {
                s = s + "0";
            }

            return isLeft ? s + str : str + s;
        } else {
            return str;
        }
    }

    public static String getInString(String str) {
        if (str == null) {
            return null;
        } else {
            StringBuffer sb = new StringBuffer("");
            String[] s = str.split(",");
            if (s != null && s.length > 0) {
                for(int i = 0; i < s.length; ++i) {
                    if (i != 0) {
                        sb.append(",");
                    }

                    sb.append("'").append(s[i]).append("'");
                }
            }

            return sb.toString();
        }
    }

    public static String subInStringByFlag(String str, String flag) {
        if (isBlank(str)) {
            return null;
        } else {
            StringBuffer sb = new StringBuffer(str);
            int index = str.lastIndexOf(flag);
            if (index < 0) {
                return str;
            } else {
                str = sb.delete(sb.length() - flag.length(), sb.length()).toString();
                index = str.indexOf(flag);
                return index < 0 ? str : sb.deleteCharAt(0).toString();
            }
        }
    }

    public static String getLastStr(String str, String flag) {
        if (isBlank(str)) {
            return null;
        } else {
            int index = str.lastIndexOf(flag);
            return index < 0 ? str : str.substring(index + flag.length());
        }
    }

    public static String getRegexStr(String str) {
        String ret = "";
        if (isBlank(str)) {
            return "";
        } else {
            if (str.indexOf(36, 0) > -1) {
                while(str.length() > 0) {
                    if (str.indexOf(36, 0) > -1) {
                        ret = ret + str.subSequence(0, str.indexOf(36, 0));
                        ret = ret + "\\$";
                        str = str.substring(str.indexOf(36, 0) + 1, str.length());
                    } else {
                        ret = ret + str;
                        str = "";
                    }
                }
            } else {
                ret = str;
            }

            return ret;
        }
    }

    public static String filterNextLine(String str) {
        return isBlank(str) ? "" : str.replaceAll("[\n\r\u0085\u2028\u2029]", "");
    }

    public static String lpad(String str, int length) {
        if (isBlank(str)) {
            return "";
        } else {
            int j = str.length();

            for(int i = j; i < length; ++i) {
                str = "0" + str;
            }

            return str;
        }
    }

    public static String getStringFilter(String str) {
        if (str != null && !"".equals(str)) {
            Pattern p = Pattern.compile("\r\n");
            Matcher m = p.matcher(str);
            String strNoBlank = m.replaceAll("");
            return strNoBlank;
        } else {
            return str;
        }
    }

    public static String splitString(String str, String parName) {
        if (str == null) {
            return null;
        } else {
            String[] parm = str.split("&");
            String parmVal = "";

            for(int i = 0; i < parm.length; ++i) {
                String[] parmArr = parm[i].split("=");
                String pname = parmArr[0];
                int parmLen = parmArr.length;
                if (parName.equals(pname)) {
                    if (parmLen == 2) {
                        parmVal = parmArr[1];
                    } else {
                        parmVal = "";
                    }
                }
            }

            return parmVal;
        }
    }

    public static String toString(String[] strAry) {
        String str = "";
        if (strAry != null) {
            for(int i = 0; i < strAry.length; ++i) {
                str = str + "," + strAry[i];
            }

            if (strAry.length > 0) {
                str = str.substring(1);
            }
        }

        return str;
    }

    public static String[] fetchStr(String str, String splitStr) {
        if (str == null) {
            return null;
        } else {
            String[] ary = str.split(splitStr);
            return ary.length < 1 && isBlank(ary[0]) ? null : ary;
        }
    }

    public static String getErrorString(Exception e) {
        return getErrorString(e, 800);
    }

    public static String getErrorString(Exception e, int maxLength) {
        if (e != null && maxLength > 0) {
            StringBuffer sb = new StringBuffer();
            sb.append(e.toString());
            if (e.getStackTrace() != null && e.getStackTrace().length > 0) {
                sb.append(", occured at ");
                sb.append(e.getStackTrace()[0].toString());
            }

            if (e.getCause() != null) {
                sb.append("; caused by ");
                sb.append(e.getCause().toString());
                if (e.getCause().getStackTrace() != null && e.getCause().getStackTrace().length > 0) {
                    sb.append(", occured at ");
                    sb.append(e.getCause().getStackTrace()[0].toString());
                }
            }

            return sb.length() > maxLength ? sb.substring(0, maxLength) : sb.toString();
        } else {
            return "";
        }
    }

    public static String subString(String str, int srcStart, int len) {
        if (isBlank(str)) {
            return null;
        } else if (srcStart >= 0 && len >= 0 && srcStart <= len) {
            byte[] bs = str.getBytes();
            int relLen = len;
            if (len > bs.length) {
                relLen = bs.length;
            }

            byte[] dest = new byte[relLen];
            System.arraycopy(bs, srcStart, dest, 0, relLen);
            return new String(dest);
        } else {
            return null;
        }
    }

    public static String getMD5(String originStr) {
        String result = "";
        if (isNotBlank(originStr)) {
            char[] md5String = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

            try {
                byte[] btInput = originStr.getBytes();
                MessageDigest mdInst = MessageDigest.getInstance("MD5");
                mdInst.update(btInput);
                byte[] md = mdInst.digest();
                int j = md.length;
                char[] str = new char[j * 2];
                int k = 0;

                for(int i = 0; i < j; ++i) {
                    byte byte0 = md[i];
                    str[k++] = md5String[byte0 >>> 4 & 15];
                    str[k++] = md5String[byte0 & 15];
                }

                result = new String(str);
            } catch (NoSuchAlgorithmException var11) {
                var11.printStackTrace();
            }
        }

        return result.trim();
    }

    public static String getChinese(String str) {
        String reg = "[^一-龥]";
        str = str.replaceAll(reg, "");
        return str;
    }

    public static String numberToChinese(String str) {
        String[] s1 = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        StringBuffer strSB = new StringBuffer();
        char[] charStr = str.toCharArray();

        for(int i = 0; i < charStr.length; ++i) {
            String s = Character.toString(charStr[i]);
            if (isNumeric(s)) {
                strSB.append(s1[Integer.parseInt(s)]);
            } else {
                strSB.append(s);
            }
        }

        return strSB.toString();
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    public static boolean isLetter(String str) {
        String reg = "[a-zA-Z]";
        boolean isLet = str.matches(reg);
        return isLet;
    }

    public static String letterToSBC(String str) {
        StringBuffer strSB = new StringBuffer();
        char[] charStr = str.toCharArray();

        for(int i = 0; i < charStr.length; ++i) {
            String s = Character.toString(charStr[i]);
            if (isLetter(s)) {
                strSB.append(ToSBC(s));
            } else {
                strSB.append(s);
            }
        }

        return strSB.toString();
    }

    public static String ToSBC(String input) {
        if (input != null) {
            char[] c = input.toCharArray();

            for(int i = 0; i < c.length; ++i) {
                if (c[i] == ' ') {
                    c[i] = 12288;
                } else if (c[i] < 127) {
                    c[i] += 'ﻠ';
                }
            }

            return new String(c);
        } else {
            return input;
        }
    }

    public static String ToDBC(String input) {
        if (input != null) {
            char[] c = input.toCharArray();

            for(int i = 0; i < c.length; ++i) {
                if (c[i] == 12288) {
                    c[i] = ' ';
                } else if (c[i] > '\uff00' && c[i] < '｟') {
                    c[i] -= 'ﻠ';
                }
            }

            input = new String(c);
        }

        return input;
    }

    public static long ipStrToLong(String ip) {
        String[] ips = ip.split("\\.");
        return getLong(ips[0]) * 256L * 256L * 256L + getlong(ips[1]) * 256L * 256L + getlong(ips[2]) * 256L + getlong(ips[3]);
    }

    public static String randomPassword() {
        StringBuffer stringBuffer = new StringBuffer();
        Random random = new Random((new Date()).getTime());
        int length = 8;
        int j = 0;

        for(int i = 0; i < length; ++i) {
            if (i == 0) {
                stringBuffer.append(upperWord[random.nextInt(upperWord.length)]);
            } else {
                int randomInt = random.nextInt(4);
                if (randomInt % 4 == 0) {
                    stringBuffer.append(upperWord[random.nextInt(upperWord.length)]);
                } else if (randomInt % 4 == 1) {
                    stringBuffer.append(lowerWord[random.nextInt(lowerWord.length)]);
                } else if (randomInt % 4 == 2) {
                    stringBuffer.append(num[random.nextInt(num.length)]);
                } else if (randomInt % 4 == 3 && j < 2) {
                    stringBuffer.append(symbol[random.nextInt(symbol.length)]);
                    ++j;
                } else {
                    stringBuffer.append(lowerWord[random.nextInt(lowerWord.length)]);
                }
            }
        }

        return stringBuffer.toString();
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }

        return dest;
    }

    public static String replaceNowString(String str) {
        return str.replace("<YYYYMM>", yyyyMM.format(new Date())).replace("<yyyyMM>", yyyyMM.format(new Date())).replace("<YYYYmm>", yyyyMM.format(new Date())).replace("<yyyymm>", yyyyMM.format(new Date())).replace("<YYYYMMDD>", yyyyMMdd.format(new Date())).replace("<yyyymmdd>", yyyyMMdd.format(new Date())).replace("<yyyyMMdd>", yyyyMMdd.format(new Date())).replace("<YYYYMMdd>", yyyyMMdd.format(new Date())).replace("<YYYYmmdd>", yyyyMMdd.format(new Date())).replace("<YYYYmmDD>", yyyyMMdd.format(new Date())).replace("<yyyyMMDD>", yyyyMMdd.format(new Date()));
    }
}